package com.xjj.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.xjj.entity.*;
import com.xjj.feign.UpLoadFeignClient;
import com.xjj.service.DiscussService;
import com.xjj.service.PhotoService;
import com.xjj.util.utils.JwtUtil;
import com.xjj.util.utils.OssManagerUtil;
import com.xjj.util.utils.RandomUtil;
import com.xjj.vo.DiscussVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.List;

/**
 * @ClassName PhotoController
 * @Description TODO
 * @Author 于文龙
 * @Date 2022/10/20 14:13
 * @Version 1.0
 **/
@RequestMapping("/photo")
@RestController
public class PhotoController {
    @Resource
    private PhotoService photoService;
    @Resource
    private DiscussService discussService;
    @Resource
    private UpLoadFeignClient upLoadFeignClient;

    //上传图片
    @RequestMapping("/admin/upload")
    public ResponseEntity<String> upload( MultipartFile file){
        return upLoadFeignClient.upload(file);
    }
    @RequestMapping("/upload")
    public ResponseEntity uploadImg(MultipartFile file) throws IOException {
        if (file == null || file.isEmpty()) {
            return ResponseEntity.FAIL("文件找不到", null);
        }
        // 获取图片后缀
        String submittedFileName = file.getOriginalFilename();
        String suffix = submittedFileName.substring(submittedFileName.lastIndexOf("."));
        // 图片随机前缀
        String fileName = RandomUtil.getImgRandom();
        // 拼接文件名
        String url = OssManagerUtil.uploadImage(fileName + suffix, file.getSize(), file.getInputStream());
        return ResponseEntity.SUCCESS("上传成功", url);
    }


    //根据发送者（班主任）id查询相册(查看自己发的相册)分页查询
    @GetMapping("/admin/all/{id}/{pageNum}/{sizeNum}")
    public ResponseEntity<PageBean<Photo>> getAllByMangerId(@PathVariable(name = "id") int id,
                                                            HttpServletRequest request,
                                                            @PathVariable(name = "pageNum") int pageNum,
                                                            @PathVariable(name = "sizeNum") int sizeNum) throws JsonProcessingException {
        Manager manager = JwtUtil.getManager(request.getHeader("token"));
        id = manager.getId();
        PageBean<Photo> pageBean = photoService.getAllPhotoByMangerId(id, pageNum, sizeNum,0);
        return ResponseEntity.SUCCESS("分页查询成功",pageBean);
    }

    //班主任模糊查询相册
    @PostMapping("/admin/like/{pageNum}/{sizeNum}")
    public ResponseEntity<PageBean<Photo>> getByTimeOrContent(@RequestBody Photo photo,
                                                              HttpServletRequest request,
                                                              @PathVariable(name = "pageNum") int pageNum,
                                                              @PathVariable(name = "sizeNum") int sizeNum) throws JsonProcessingException {
        Manager manager = JwtUtil.getManager(request.getHeader("token"));
        photo.setManagerId(manager.getId());
        PageBean<Photo> photoList = photoService.getByTimeOrContent(photo,pageNum,sizeNum);
        return ResponseEntity.SUCCESS("模糊查询成功",photoList);
    }

    //班主任根据相册id删除相册
    @PostMapping("/admin/delete")
    public ResponseEntity<String> deleteByPhotoId(@RequestBody Photo photo){
        photoService.deletePhoto(photo);
        return ResponseEntity.SUCCESS("班主任删除相册成功",null);
    }

    //班主任发布相册
    @PostMapping("/admin/add")
    public ResponseEntity<String> addPhoto(@RequestBody Photo photo,
                                           HttpServletRequest request) throws JsonProcessingException {
        Manager manager = JwtUtil.getManager(request.getHeader("token"));
        photo.setManagerId(manager.getId());
        photoService.addPhoto(photo);
        return ResponseEntity.SUCCESS("班主任发布相册成功",null);
    }

    //班主任评论相册（班主任不能评论，冇用）
/*    @GetMapping("/admin/adddiscuss")
    public ResponseEntity<Discuss> adminAddDiscuss(@RequestBody Discuss discuss) {
        Discuss discuss1 = discussService.add(discuss);
        return ResponseEntity.SUCCESS("评论成功", discuss1);
    }*/
    //班主任查看评论
    @GetMapping("/admin/alldiscuss/{id}")
    public ResponseEntity<List<Discuss>> findAllDisscussByPhotoId(@PathVariable(name = "id") int id) {
        List<Discuss> discusses = discussService.findAllDisscussByPhotoId(id);
        return ResponseEntity.SUCCESS("老师查看评论成功", discusses);
    }

    //家长查看评论
    @GetMapping("/api/alldiscuss/{id}")
    public ResponseEntity<List<Discuss>> findAllDisscussByMomentId(@PathVariable(name = "id") int id) {
        List<Discuss> discusses = discussService.findAllDisscussByPhotoId(id);
        return ResponseEntity.SUCCESS("评论组件需求信息", discusses);
    }

    //家长添加评论
    @PostMapping("/api/adddiscuss")
    public ResponseEntity<Discuss> addDiscuss(@RequestBody Discuss discuss) {
        Discuss discuss1 = discussService.add(discuss);
        return ResponseEntity.SUCCESS("评论成功", discuss1);
    }

    //家长删除评论
    @DeleteMapping("/api/deleteDiscuss/{discussId}/{photoId}")
    public ResponseEntity<String> deleteDiscuss(@PathVariable(name = "discussId") int discussId,
                                                @PathVariable(name = "photoId") int photoId){
        discussService.deleteById(discussId,photoId);
        return ResponseEntity.SUCCESS("删除评论成功",null);
    }

    //家长点赞相册
    @PostMapping("/api/like/{photoId}")
    public ResponseEntity<String> likephoto(@PathVariable int photoId,
                                            HttpServletRequest request) throws JsonProcessingException {
        String token = request.getHeader("token");
        Parent parent = JwtUtil.getParent(token);
        Integer parentId = parent.getId();
        //相册id和家长id（判断哪个家长给哪个相册点赞）
        photoService.like(photoId,parentId);
        return ResponseEntity.SUCCESS("操作成功（点赞）",null);
    }

    //家长查看相册详情
    @GetMapping("api/detail/{photoId}")
    public ResponseEntity<Photo> getDetail(@PathVariable int photoId,
                                           HttpServletRequest request) throws JsonProcessingException {
        String token = request.getHeader("token");
        Parent parent = JwtUtil.getParent(token);
        Integer parentId = parent.getId();
        Photo byPhotoId = photoService.getByPhotoId(photoId, parentId);
        return ResponseEntity.SUCCESS("查看相册详情",byPhotoId);
    }

    //家长查看班级所有相册
    @GetMapping("api/allphoto/{studentId}/{pageNum}/{sizeNum}")
    public ResponseEntity<PageBean<Photo>> getByStudentId(@PathVariable(name = "studentId") int studentId,
                                                            HttpServletRequest request,
                                                            @PathVariable(name = "pageNum") int pageNum,
                                                            @PathVariable(name = "sizeNum") int sizeNum) throws JsonProcessingException {
        //获取学生id（todo）
        studentId = 1;
        String token = request.getHeader("token");
        Parent parent = JwtUtil.getParent(token);
        Integer parentId = parent.getId();
        PageBean<Photo> pageBean = photoService.getAllPhotoByMangerId(studentId, pageNum, sizeNum,parentId);
        return ResponseEntity.SUCCESS("分页查询成功",pageBean);
    }
}
